package naga.x.platform2.utils;

import java.util.ArrayList;
import java.util.List;

public class StringUtils {

	private StringUtils() {}
	
	public static boolean isEmpty(String str) {
		return str == null || str.trim().length() == 0;
	}
	
	public static boolean isNotEmpty(String str) {
		return str != null && str.trim().length() > 0;
	}
	
	public static String[] split(String src, String separator) {
		return split(src, separator, false);
	}
	
	public static String[] split(String src, String separator, boolean regex) {
		if (isEmpty(src)) {
			return null;
		}
		if (regex) {
			return src.split(separator);
		}
		int len = src.length();
		int sepLen = separator.length();
		List<String> result = new ArrayList<String>();
		for (int from = 0, to = 0; from < len; from = to + sepLen) {
			to = src.indexOf(separator, from);
			if (to < 0) {
				to = len;
			}
			String s = src.substring(from, to);
			result.add(s);
		}
		return result.toArray(new String[result.size()]);
	}
	
	public static String bytesToString(byte[] b, String charset) {
		if (b == null || b.length == 0) {
			return null;
		}
		try {
			return new String(b, charset);
		} catch (Exception e) {
			return null;
		}
	}
	
	public static String trim(String s) {
		return s != null ? s.trim() : "";
	}
	
	public static String maskNull(String s) {
		return s != null ? s : "";
	}
	
}
